package com.myj.jim.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.myj.jim.dao.OperatorDao;
import com.myj.jim.domain.Operator;
import com.myj.jim.domain.OperatorRole;
import com.myj.jim.domain.OperatorRoleAuthority;
import com.myj.jim.domain.bootstraptable.BootstrapTable;
import com.myj.jim.domain.bootstraptable.BootstrapTableJSON;
import com.myj.jim.service.OperatorService;
import com.myj.jim.util.MD5Utils;

@Service
public class OperatorServiceImpl implements OperatorService {
	
	@Autowired
	private OperatorDao operatorDao;

	@Override
	public BootstrapTableJSON<Operator> findPage(Map<String,Object> map) {
		List<Operator> list = operatorDao.findPage(map);
		int total = operatorDao.count(map);
		BootstrapTableJSON<Operator> btj = new BootstrapTableJSON<Operator>();
		btj.setTotal(total);
		btj.setRows(list);
		return btj;
	}

	@Override
	public boolean save(Operator op, String roleSelectionsJson) {
		op.setPassword(MD5Utils.encode(op.getPassword()));
		int ret = operatorDao.insert(op);
		
		if(roleSelectionsJson!=null && !"".equals(roleSelectionsJson)){
			JSONArray arr = JSONArray.fromObject(roleSelectionsJson);
			
			Map<String, String> map = new HashMap<String, String>();
			
			for(Object o : arr){
				map.put("loginid", op.getLoginid());
				map.put("roleno", JSONObject.fromObject(o).getString("roleno"));
				operatorDao.insertOperatorRole(map);
			}
		}
		
		return ret!=0;
	}

	@Override
	public boolean update(Operator op, String roleSelectionsJson) {
		int ret = operatorDao.updateByPrimaryKey(op);
		
		operatorDao.deleteOperatorRoleByLoginid(op.getLoginid());
		
		if(roleSelectionsJson!=null && !"".equals(roleSelectionsJson)){
			JSONArray arr = JSONArray.fromObject(roleSelectionsJson);
			
			Map<String, String> map = new HashMap<String, String>();
			
			for(Object o : arr){
				map.put("loginid", op.getLoginid());
				map.put("roleno", JSONObject.fromObject(o).getString("roleno"));
				operatorDao.insertOperatorRole(map);
			}
		}
		
		return ret!=0;
	}

	@Override
	public boolean isExist(String loginid) {
		return operatorDao.isExist(loginid)>0;
	}

	@Override
	public void delete(Integer[] ids) {
		
		for(Integer id : ids){
			operatorDao.deleteByPrimaryKey(id);
		}
		
	}

	@Override
	public Operator login(Operator op) {
		op.setPassword(MD5Utils.encode(op.getPassword()));
		
		op = operatorDao.selectOperatorByLoginidAndPassword(op);
		
		return op;
	}

	@Override
	public List<OperatorRole> getRoleListByLoginid(String loginid) {
		return operatorDao.selectOperatorRoleByLoginid(loginid);
	}

	@Override
	public List<OperatorRoleAuthority> selectOperatorRoleAuthorityByLoginid(
			String loginid) {
		return operatorDao.selectOperatorRoleAuthorityByLoginid(loginid);
	}

	@Override
	public List<Operator> getOperatorListByIds(Integer[] idsArr) {
		String ids = "";
		
		for(int i=0;i<idsArr.length;i++){
			if(i>0){
				ids += ",";
			}
			ids += "'"+idsArr[i]+"'";
		}
		
		Map<String, String> map = new HashMap<String,String>();
		map.put("ids", ids);
		
		return operatorDao.getOperatorListByIds(map);
	}


	
	
}
